package com.example.intermediate.controller;

import com.example.intermediate.service.MqttPublishService;
import com.example.intermediate.service.MqttSubscribeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @program: my-medical
 * @author: liuQi
 * @create: 2025-05-09 14:57
 */
@RestController
@RequestMapping("/text")
public class TestController {


    @Autowired
    private MqttPublishService mqttPublishService;

    @Autowired
    private MqttSubscribeService mqttSubscribeService;

    @GetMapping("/publish")
    public String publishMessage(@RequestParam String message) {
        try {
            mqttPublishService.publish(message);
            return "消息发布成功";
        } catch (Exception e) {
            return "消息发布失败：" + e.getMessage();
        }
    }

    @GetMapping("/subscribe")
    public String subscribeTopic() {
        try {
            mqttSubscribeService.subscribe();
            return "订阅成功";
        } catch (Exception e) {
            return "订阅失败：" + e.getMessage();
        }
    }
}
